package com.wc.AlgoOJ_homework.ALOJ模块测试Ⅱ.E_三升序列;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/16 15:10
 * @description http://43.138.190.70:8888/p/LQ1054T?tid=663e2cd8d39d10926ed3fa74
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510, M = 30;
    static char[][] g = new char[N][];
    static char[] s = new char[N];
    static int[] tr = new int[M];
    static int[] lenum = new int[M];
    static long[] l = new long[N], r = new long[N];
    static int n, m;
    static long res = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) g[i] = (" " + sc.next()).toCharArray();
        // every row
        for (int i = 1; i <= n; i++) {
            work(g[i], l, r, m);
        }
        // every col
        for (int j = 1; j <= m; j++) {
            for (int i = 1; i <= n; i++) s[i] = g[i][j];
            work(s, l, r, n);
        }

        // \down
        for (int i = 1; i <= m; i++) {
            int x = 1, y = i;
            int len = 0;
            while (x <= n && y <= m) {
                s[++len] = g[x][y];
                x++;
                y++;
            }
            work(s, l, r, len);
        }
        for (int i = 2; i <= n; i++) {
            int x = i, y = 1, len = 0;
            while (x <= n && y <= m) {
                s[++len] = g[x][y];
                x++;
                y++;
            }
            work(s, l, r, len);
        }

        // /down
        for (int i = 1; i <= m; i++) {
            int x = 1, y = i, len = 0;
            while (x <= n && y >= 1) {
                s[++len] = g[x][y];
                x++;
                y--;
            }
            work2(s, l, r, len);
        }
        for (int i = 2; i <= n; i++) {
            int x = i, y = m, len = 0;
            while (x <= n && y >= 1) {
                s[++len] = g[x][y];
                x++;
                y--;
            }
            work2(s, l, r, len);
        }

        // /up
        for (int i = 1; i <= m; i++) {
            int x = n, y = i, len = 0;
            while (x >= 1 && y <= m) {
                s[++len] = g[x][y];
                x--;
                y++;
            }
            work2(s, l, r, len);
        }
        for (int i = 1; i < n; i++) {
            int x = i, y = 1, len = 0;
            while (x >= 1 && y <= m) {
                s[++len] = g[x][y];
                x--;
                y++;
            }
            work2(s, l, r, len);
        }

        out.println(res);
        out.flush();
    }

    static void work(char[] s, long[] l, long[] r, int n) {
        Arrays.fill(l, 1, n + 1, 0);
        Arrays.fill(r, 1, n + 1, 0);

        for (int i = 1; i <= n; i++) {
            char c = s[i];
            for (int j = 'A'; j < c; j++) {
                l[i] += lenum[j - 'A' + 1];
            }
            lenum[c - 'A' + 1]++;
        }

        for (int i = 1; i <= n; i++) {
            char c = s[i];
            for (int j = c + 1; j <= 'Z'; j++) {
                r[i] += lenum[j - 'A' + 1];
            }
            lenum[c - 'A' + 1]--;
        }

        for (int i = 1; i <= n; i++) {
            res += l[i] * r[i];
        }
    }

    static void work2(char[] s, long[] l, long[] r, int n) {
        Arrays.fill(tr, 0);
        for (int i = 1; i <= n; i++) {
            int x = s[i] - 'A' + 1;
            l[i] = sum(x - 1);
            add(x, 1);
        }
        Arrays.fill(tr, 0);
        for (int i = n; i >= 1; i--) {
            int x = s[i] - 'A' + 1;
            res += l[i] * (sum(M - 1) - sum(x));
            add(x, 1);
        }
    }

    static int lowbit(int x) {
        return x & -x;
    }

    static void add(int x, int c) {
        for (int i = x; i < M; i += lowbit(i)) {
            tr[i] += c;
        }
    }


    static long sum(int x) {
        long res = 0;
        for (int i = x; i > 0; i -= lowbit(i)) {
            res += tr[i];
        }
        return res;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
